package com.mmm.controller;

import com.mmm.exception.AccountOverDrawnException;
import com.mmm.exception.invalidDepositException;
import com.mmm.pojo.Users;
import com.mmm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;
import java.util.List;


/**
 * @author mmm
 * @date 2021/9/22 - 17:11
 */
@Controller
public class UserController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    //跳转到用户操作页面或登录失败页面
   @RequestMapping(value = "/login")
    private String login(String userName,String password ,Model Model,HttpServletRequest servletRequest){
      Users users=userService.login(userName,password);
       System.out.println(users);
      if (users==null) {
         return "error";
      }
     int userId=users.getUserId();
      Model.addAttribute("userId",userId);
       HttpSession session = servletRequest.getSession();
       session.setAttribute("userName",userName);
       return "login";
   }
   //跳转到管理员登录页面
   @RequestMapping(value = "/adminLogin")
    private String adminLogin(){
       return "adminLogin";
    }

    //跳转到管理员操作页面
    @RequestMapping(value = "/doAdmin")
    private String doAdmin(String userName,String password){
       Users users=userService.login(userName,password);
        System.out.println(users);
       if (users==null||users.getUserType()==0){
           return "error";
       }
       return "doAdmin";
    }

    //跳转到注册页面
    @RequestMapping(value = "/register")
    private String register(){
        return "register";
    }

    //注册成功或者注册失败后跳转页面
    @RequestMapping(value = "/doRegister")
    private String doRegister(String userName,String password,double balance){
      boolean flag=userService.register(userName,password,balance);
      if (flag==false){
          return "error";
      }
       return "success";
    }

    //退出系统
    @RequestMapping(value = "/exit")
    private String exitSystem(){
       return "exitSuccess";
    }
//跳转到用户操作页面
    @RequestMapping(value = "/doInquiry")
    private String doInquiry(){
       return "login";
   }
//查询所有用户信息
@RequestMapping(value = "/selectAllUser")
    private  String selectAllUser(Model model){
      List<Users> list =userService.findAllUser();
      model.addAttribute("list",list);
       return "selectAllUser";
}
//返回管理员操作界面
    @RequestMapping(value = "/reAdmin")
    private String reAdmin(){
       return "doAdmin";
    }
  //删除用户
    @RequestMapping(value = "/deleteUser/{userId}")
    private String deleteUser(@PathVariable("userId") int userId){
       userService.deleteUser(userId);
       return "redirect:/selectAllUser";
    }
    //冻结用户
    @RequestMapping(value = "/frozen/{userId}")
    private String frozen(@PathVariable("userId") int userId){
       userService.frozenAccount(userId);
       return "redirect:/selectAllUser";
    }
    //跳转到转账页面
    @RequestMapping(value = "/transfer")
    private String transfer(){
       return "transfer";
    }
    //转账成功返回用户操作界面，失败跳转到错误页面
    @RequestMapping(value = "/doTransfer")
    private  String doTransfer( String userName,double balance,String transferName,HttpServletRequest servletRequest) {
        String curUserName=(String) servletRequest.getSession().getAttribute("userName");
        if (!curUserName.equals(userName)){
            return "error";
        }
       int status=userService.Transfer(userName,balance,transferName);
       if (status==0){
           return "error";
       }
      Users users= userService.selectUserByUserName(userName);
       servletRequest.setAttribute("userId",users.getUserId());
       return "login";
    }
    //跳转到取款页面
    @RequestMapping(value = "/withdrawals")
    private  String withdrawals(){
       return "withdrawals";
    }
    //取款操作
    @RequestMapping(value = "/doWithdrawals")
    private String doWithdrawals(String userName,double balance,HttpServletRequest servletRequest) throws AccountOverDrawnException {
       String curUserName=(String) servletRequest.getSession().getAttribute("userName");
       if (!curUserName.equals(userName)){
           return "error";
       }
       int status=userService.withdrawals(userName,balance);
       if (status==0){
           return "error";
       }
       Users users=userService.selectUserByUserName(userName);
       servletRequest.setAttribute("userId",users.getUserId());
       return "login";
    }
    //跳转到存款页面
    @RequestMapping(value = "/deposit")
    private String deposit(){
       return "deposit";
    }
    //跳转到查询页面
    @RequestMapping(value = "/inquiry")
    private String inquiry(String userId,HttpServletRequest servletRequest){
        double balance=userService.inquiry(Integer.parseInt(userId));
        servletRequest.setAttribute("balance",balance);
        return "inquiry";
    }
//    private String doDeposit(String userName,double balance,HttpServletRequest servletRequest){
//    }
   // 存款操作
    @RequestMapping(value = "/doDeposit")
    private  String doDeposit(String userName,double balance ,HttpServletRequest servletRequest,Model model) throws invalidDepositException {
        String curUserName=(String) servletRequest.getSession().getAttribute("userName");
        if (!curUserName.equals(userName)){
            return "error";
        }
      int status=userService.deposit(userName,balance);
      if (status==0){
          return "error";
      }
        System.out.println("username======="+userName);
      Users users=userService.selectUserByUserName(userName);
        System.out.println(users);
      //servletRequest.setAttribute("userId",users.getUserId());
        model.addAttribute("userId",users.getUserId());
       return "login";
    }

}

